/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ems.entity.EmsArea;
import com.thinkgem.jeesite.modules.ems.entity.EmsTerm;
import com.thinkgem.jeesite.modules.ems.service.EmsAreaService;
import com.thinkgem.jeesite.modules.ems.service.EmsTermService;

/**
 * 学期Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsTerm")
public class EmsTermController extends BaseController {

	@Autowired
	private EmsTermService emsTermService;
	@Autowired
	private EmsAreaService emsAreaService;
	
	@ModelAttribute
	public EmsTerm get(@RequestParam(required=false) String id) {
		EmsTerm entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsTermService.get(id);
		}
		if (entity == null){
			entity = new EmsTerm();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsTerm:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsTerm emsTerm, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmsTerm> page = emsTermService.findPage(new Page<EmsTerm>(request, response), emsTerm);
		
		// add by zad 2018-01-25 区域查询
		List<EmsArea> areas = this.emsAreaService.findList(null);
		model.addAttribute("areas", areas);
		
		model.addAttribute("page", page);
		return "modules/ems/emsTermList";
	}

	@RequiresPermissions("ems:emsTerm:view")
	@RequestMapping(value = "form")
	public String form(EmsTerm emsTerm, Model model) {
		
		// add by zad 2018-01-25 区域查询
		List<EmsArea> areas = this.emsAreaService.findList(null);
		model.addAttribute("areas", areas);
		
		model.addAttribute("emsTerm", emsTerm);
		return "modules/ems/emsTermForm";
	}

	@RequiresPermissions("ems:emsTerm:edit")
	@RequestMapping(value = "save")
	public String save(EmsTerm emsTerm, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsTerm)){
			return form(emsTerm, model);
		}
		emsTermService.save(emsTerm);
		addMessage(redirectAttributes, "保存学期成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsTerm/?repage";
	}
	
	@RequiresPermissions("ems:emsTerm:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsTerm emsTerm, RedirectAttributes redirectAttributes) {
		emsTermService.delete(emsTerm);
		addMessage(redirectAttributes, "删除学期成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsTerm/?repage";
	}

}